﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Fcdbas.WinUI.Forms;
using Fcdbas.Core.DataAccess;
using Fcdbas.Core.Xml;
using Fcdbas.Core.Windows;
using Fcdbas.Platform;
using Fcdbas.WinUI.Common;
using Fcdbas.Core;
namespace Fcdbas.WinApp
{
    public partial class frmOrder : cFormWindow
    {
        public frmOrder()
        {
            InitializeComponent();
           // PropertyDescriptor [] cc = new PropertyDescriptor[100];
          //  MasterdataSourceBinding.DataSource = new Fcdbas.CustomerOrder.CUSTOMER_ORDER();
           // PropertyDescriptorCollection list =  this.MasterdataSourceBinding.GetItemProperties(null);
        }

        private void cButton1_Click(object sender, EventArgs e)
        {
            SalWindow.FireWindowActions(this, Const.PM_DataRecordNew, 1, 0);
        }

        private void cButton2_Click(object sender, EventArgs e)
        {
            SalWindow.FireWindowActions(this, Const.PM_DataSourceSave, 1, 0);
            BaseForm form = this.MainForm.OpenModule("key", "SQL where");
        }

        private void frmOrder_Load(object sender, EventArgs e)
        {
            this.DataItemLovUserWhere += new EventHandler<ReturnEventArgs<SalString>>(frmOrder_DataItemLovUserWhere);
            //this.cRecSelExtComboBox1.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
            //new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
            //this.cRecSelExtComboBox1.Properties.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
            //new DevExpress.XtraEditors.Controls.LookUpColumnInfo("order_no", "订单号:"),
            //new DevExpress.XtraEditors.Controls.LookUpColumnInfo("contract", "合同:"),
            //new DevExpress.XtraEditors.Controls.LookUpColumnInfo("customer_no", "客户号:")});
            //this.cRecSelExtComboBox1.Properties.DataSource = this.MasterdataSourceBinding;
            //this.cRecSelExtComboBox1.Properties.DisplayMember = "order_no";
            //this.cRecSelExtComboBox1.Properties.ValueMember = "order_no";
            //this.cRecSelExtComboBox1.Properties.DataSource = Fcdbas.Core.Entity<CustomerOrder.customer_order>.FindAll();
            
            //this.txtOrderNo.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.MasterdataSourceBinding, "order_no"));
            //this.txtContrat.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.MasterdataSourceBinding, "contract"));
            //this.txtCustomerNO.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.MasterdataSourceBinding, "customer_no"));
            //this.txtOrderAmount.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.MasterdataSourceBinding, "order_amount"));
            //this.deDelivery.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.MasterdataSourceBinding, "delivery_date"));

           // this.cCheckBox1.DataBindings.Add(new System.Windows.Forms.Binding("EditValue", this.MasterdataSourceBinding, "contract"));
            Console.Write("test");
        }

        void frmOrder_DataItemLovUserWhere(object sender, ReturnEventArgs<SalString> e)
        {
            e.ReturnValue = "order_no ='ABC'";
        }

        private void cButton3_Click(object sender, EventArgs e)
        {
            //this.MergeMenu(
        }

        private void cButton1_Click_1(object sender, EventArgs e)
        {
            //Console.WriteLine(dfsOrder_No.Text);
            //Console.WriteLine(dfsCustomer_No.Text);
            //Console.WriteLine(dfsContract.Text);
        }

        private void abcfToolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        private void chkContract_CheckedChanged(object sender, EventArgs e)
        {
            Console.WriteLine("checked");
        }

        private void chkContract_CheckStateChanged(object sender, EventArgs e)
        {
            Console.WriteLine("chkContract_CheckStateChanged");
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            cSessionManager.DbTransactionBegin();
            Dictionary<SalString, object> dic = new Dictionary<SalString, object>();
            IEntity entity =  this.MasterdataSourceBinding.Current as IEntity ;
            String __lsResult = "";
            dic.Add("info_", __lsResult);
            if (entity["objid"] != null)
                dic.Add("objid_", entity["objid"].ToString());
            else
                dic.Add("objid_", "");
            if (entity["objversion"] != null)
                //dic.Add("objversion_", ((DateTime)entity["objversion"]).ToString("yyyy-MM-dd HH:mm:ss"));
                dic.Add("objversion_", entity["objversion"].ToString());
            else
                dic.Add("objversion_", "");
           
            dic.Add("attr_", entity.GetAttr(false));
            dic.Add("action_", "Check");
            SalString errorMsg = "";
            SalNumber errorCode = 0;
            SalNumber res = cSessionManager.DbMySQLBlock("CustomerOrder_New__( :info_, :objid_, :objversion_, :attr_, :action_ )", dic, CommandType.StoredProcedure);
            if (res == 0)
            {
                errorCode = cSessionManager.GetSqlLastError(ref errorMsg);
                cSessionManager.DbTransactionClear();
                this.ShowMessage(errorMsg);
            }
            else
            {
                cSessionManager.DbTransactionEnd();
                this.ShowMessage(dic["info_"].ToString());
            }
        }

        private void MasterdataSourceBinding_DataRecordCheckNewEvent(object sender, WinUI.Controls.cDataSource.DataRecordCheckNewEventArgs e)
        {
            this.ShowMessage("DataRecordCheckNewEvent");
        }

        private void MasterdataSourceBinding_DataRecordEditEvent(object sender, WinUI.Controls.cDataSource.DataRecordEditEventArgs e)
        {

        }
        /// <summary>
        /// 新建记录时调用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MasterdataSourceBinding_DataRecordNewEvent(object sender, WinUI.Controls.cDataSource.DataRecordNewEventArgs e)
        {
            this.ShowMessage("DataRecordNewEvent");
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
           // this.MainForm.OpenModule(nav_key,)
            Dictionary<SalString, object> dic = new Dictionary<SalString, object>();
            //dic.Add("customer_no", medNote_Text.Text);
            DataSet ds= cSessionManager.dal.Select("select * from customer_order where customer_no=@customer_no",dic);
           // this.cGridControl1.DataSource = ds;
           // this.cGridControl1.RefreshDataSource();
        }

        private void MasterdataSourceBinding_DataRecordCheckRequiredEvent(object sender, WinUI.Controls.cDataSource.ReturnEventArgsSalBoolean e)
        {//sdfasdfsadfasdf
            //asdfasf
            
            
        }

        private void MasterdataSourceBinding_DataRecordExecuteNewEvent(object sender, WinUI.Controls.cDataSource.DataRecordExecuteNewEventArgs e)
        {
            string attr = e.Entity.GetAttr();
            Fcdbas.WinUI.Common.ClientSys.SetItemValue("ADDRESS", "asdflkasdjfasdf;092332",ref attr);
            e.Entity.EditAttr(attr);
            e.Handled = false;
        }
    }
}